package edu.mis.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public static void main(String[] args) throws Exception{
        String first = "1995-9-2";
        String second = "2017-3-22";
        Date firstdate = format.parse(first);
        Date seconddate = format.parse(second);
        int cnt = longOfTwoDate(firstdate, seconddate);
        int days = differentDaysByMillisecond(firstdate, seconddate);
        System.out.println(firstdate);
        System.out.println(cnt);
        System.out.println(days);
    }

    public static int longOfTwoDate(Date firstdate, Date seconddate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(firstdate);
        int cnt = 0;
        while(calendar.getTime().compareTo(seconddate) != 0){
            calendar.add(Calendar.DATE, 1);
            cnt++;
        }
        return cnt;
    }

    public static int differentDaysByMillisecond(Date firstdate, Date seconddate){
        int days = (int) ((seconddate.getTime() - firstdate.getTime()) / (1000*60*60*24));
        return days;
    }
    public static Date newDelayDate(Date firstdate, Integer delayDays){
        long Millisecond = (delayDays != 0)?firstdate.getTime()+delayDays*1000*60*60*24:firstdate.getTime();
        firstdate.setTime(Millisecond);
        return firstdate;
    }

}
